(function () {
  const elevator = document.querySelector('.xtx-elevator')

  const backTop = document.querySelector('#backTop')
  window.addEventListener('scroll', function (e) {
    // 卷起的部分  滑动显示侧边栏
    const n = document.documentElement.scrollTop
    // offsetTop 距离自己定位父级元素上距离
    if (n >= backTop.offsetTop) {
      elevator.style.opacity = 1
    } else {
      elevator.style.opacity = 0
    }
  })
// 返回顶部
  backTop.addEventListener('click', function () {
    // document.documentElement.scrollTop = 0
    window.scrollTo(0, 0)
  })
})();
(function () {
  const elevator = document.querySelector('.xtx-elevator-list');
  const newGoods = document.querySelector('.xtx_goods_new');
  const popular = document.querySelector('.xtx_goods_popular');
  const brand = document.querySelector('.xtx_goods_brand');
  const topic = document.querySelector('.xtx_goods_topic');

  elevator.addEventListener('click', function (e) {
    // console.log(e.target.tagName)
    if (e.target.tagName === 'A') {
      const uname = e.target.dataset.name
      switch (uname) {
        case 'newGoods':
          window.scrollTo(0, newGoods.offsetTop)
          break
        case 'popular':
          window.scrollTo(0, popular.offsetTop)
          break
        case 'brand':
          window.scrollTo(0, brand.offsetTop)
          break
        case 'topic':
          window.scrollTo(0, topic.offsetTop)
          break
        default: alert('bye~') 
      }
    }
  })
// 滑动页面添加颜色
  window.addEventListener('scroll', function () {
    const n = document.documentElement.scrollTop
    // console.log(document.documentElement.scrollTop);
    elevator.querySelector('a.active') && elevator.querySelector('a.active').classList.remove('active')
    if (n >= newGoods.offsetTop && n < popular.offsetTop) {
      elevator.querySelector('a[data-name="newGoods"]').classList.add('active')
    } else if (n >= popular.offsetTop && n < brand.offsetTop) {
      elevator.querySelector('a[data-name="popular"]').classList.add('active')
    } else if (n >= brand.offsetTop && n < topic.offsetTop) {
      elevator.querySelector('a[data-name="brand"]').classList.add('active')
    } else if (n >= topic.offsetTop) {
      elevator.querySelector('a[data-name="topic"]').classList.add('active')
    }
  })
})()